package com.team3.project.dao.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;


import com.team3.project.dao.PublicHolidayDAO;
import com.team3.project.model.PublicHolidays;

public class PublicHolidayDAOImpl implements PublicHolidayDAO {

	private static final Log log = LogFactory.getLog(UserDAOImpl.class);
	private static Session session=null;
	private static Transaction tx = null;
	private static Query query =null;
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	private void shutdown() {
		// TODO Auto-generated method stub
		session.close();
	}
	
	@Override
	public void save(PublicHolidays publicHolidays) {
		// TODO Auto-generated method stub
		try {
			session= getSessionFactory().openSession();
			session.beginTransaction();
			session.save(publicHolidays);
			session.getTransaction().commit();
			
		}catch(RuntimeException e){
    		try{
    			tx.rollback();
    		}catch(RuntimeException rbe){
    			log.error("Couldn’t roll back transaction", rbe);
    		}
    		throw e;
    	}finally{
    		if(session!=null){
    			shutdown();
    		}
    	}
	}

	@Override
	public void update(PublicHolidays publicHolidays) {
		try{
    		session = getSessionFactory().openSession();
    		tx = session.beginTransaction();
    		tx.setTimeout(5);
    		session.update(publicHolidays);
    		
    		tx.commit();
 
    	}catch(RuntimeException e){
    		try{
    			tx.rollback();
    		}catch(RuntimeException rbe){
    			log.error("Couldn’t roll back transaction", rbe);
    		}
    		throw e;
    	}finally{
    		if(session!=null){
    			shutdown();
    		}
    	}
		
	}

	@Override
	public void delete(PublicHolidays publicHolidays){
		try{
    		session = getSessionFactory().openSession();
    		tx = session.beginTransaction();
    		tx.setTimeout(5);
    		session.delete(publicHolidays);
    	
    		tx.commit();
 
    	}catch(RuntimeException e){
    		try{
    			tx.rollback();
    		}catch(RuntimeException rbe){
    			log.error("Couldn’t roll back transaction", rbe);
    		}
    		throw e;
    	}
    	finally{
    		if(session!=null){
    			shutdown();
    		}
    	}
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PublicHolidays> findAll() {
		List<PublicHolidays> list =null;
		try{
    		session = getSessionFactory().openSession();
    		tx = session.beginTransaction();
    		tx.setTimeout(5);
    		
    		query = session.createQuery("From PublicHolidays");
    		list = query.list();
    	
    		tx.commit();
 
    	}catch(RuntimeException e){
    		try{
    			tx.rollback();
    		}catch(RuntimeException rbe){
    			log.error("Couldn’t roll back transaction", rbe);
    		}
    		throw e;
    	}
    	finally{
    		if(session!=null){
    			shutdown();
    		}
    	}
		return list;
	}

}
